<?php
/**
 * Provides some core helper functions. All helper functions are used in the theme as
 * custom template tags. Most of them are attached to action and filter hooks.
 *
 * When using a child theme (see http://codex.wordpress.org/Theme_Development), you can
 * override those functions (those wrapped in a function_exists() call) by defining
 * them first in your child theme's functions.php file. The child theme's functions.php
 * file is included before the parent theme's file, so the child theme functions would
 * be used.
 *
 * Or just remove added functions by using remove_action() or remove_filter() and attach
 * your own function to the hook.
 *
 * For more information on hooks, see http://codex.wordpress.org/Plugin_API.
 *
 * @package dkret3
 * @since 3.6
 */

if ( ! function_exists( 'dkret_add_access_menu' ) ) :
/**
 * Add the template part 'access-menu.php'
 * If you want to overload this in a child theme then include a file
 * called access-menu.php and that will be used instead. 
 * 
 * @since 3.6
*/
function dkret_add_access_menu() {
    get_template_part( 'access-menu', 'header' );
}
endif;
 
if ( ! function_exists( 'dkret_comments_callback' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * To override this walker in a child theme without modifying the comments template
 * simply create your own dkret_comments_callback(), and that function will be used instead.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 *
 * @since 3.6
 */
function dkret_comments_callback( $comment, $args, $depth ) {
    $GLOBALS['comment_depth'] = $depth;
    $GLOBALS['comment'] = $comment;
    ?>
    <li <?php comment_class() ?> id="comment-<?php comment_ID(); ?>">
        <div id="div-comment-<?php comment_ID(); ?>">

            <?php /* dkret3 Hook */ do_action( 'dkret_before_comment' ); ?>

            <div class="comment-text">
                <?php if ( function_exists( 'gtcn_comment_numbering' ) ) echo gtcn_comment_numbering( $comment->comment_ID, $args ); ?>

                <?php if ( $comment->comment_approved == '0' ) : ?> 
                    <p class="alert moderation"><?php _e( 'Your entry is awaiting moderation.', 'dkret' ); ?></p>
                <?php endif; ?>

                <?php echo apply_filters( 'comment_text', get_comment_text() ); ?>
            </div>
                
            <?php /* dkret3 Hook */ do_action( 'dkret_after_comment' ); ?>
            
        </div>
<?php
}
endif;

if ( ! function_exists( 'dkret_comments_end_callback' ) ) :
/**
 * Ends the display of individual comments. Uses the callback parameter for wp_list_comments(). 
 * Needs to be used in conjunction with dkret_comments_callback(). Not needed but used just in 
 * case something is changed.
 *
 * @since 3.6
 */
function dkret_comments_end_callback( $comment, $args, $depth ) {
	echo '</li><!-- .comment -->';
}
endif;

if ( ! function_exists( 'dkret_disable_styles' ) ) :
/**
 * Disables stylesheets for particular plugins to allow the theme to easily write its own
 * styles for the plugins' features.
 *
 * @since 3.6
 * @link http://wordpress.org/extend/plugins/wp-pagenavi
 */
function dkret_disable_styles() {
	/* Deregister the WP PageNavi plugin style. */
	wp_deregister_style( 'wp-pagenavi' );
	/* Deregister the WP Gregs Threaded Comment Numbering plugin style. */
    wp_deregister_style( 'gtcn-plugin' );
}
endif;

if ( ! function_exists( 'dkret_enque_scripts' ) ) :
/**
 * Function that adds CSS and Javascript to the <header>.
 * 
 * @since 3.6
*/
function dkret_enque_scripts() {

    if ( get_dkret_option( 'layout_file' ) != 'Default-Layout.css' )
        echo "\n\t<link rel=\"stylesheet\" type=\"text/css\" title=\"layout\" media=\"screen\" href=\"". get_bloginfo( 'stylesheet_directory' ) . '/library/layouts/' . get_dkret_option( 'layout_file' ) .'" />';

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'drop-downs', THEME_JS .'/drop-downs.js', array( 'jquery' ), '1.4.8', true );
    wp_enqueue_script( 'dkret-js', THEME_JS .'/dkret.js', array( 'jquery' ), '3.6', true );

    if( get_dkret_option( 'preview_comments' ) ) :
        wp_enqueue_script( 'dkret-commentpreview', THEME_JS .'/dkret-commentpreview.js', array( 'jquery' ), '3.6', true );
    endif;
    
    if( get_dkret_option( 'validate_comments' ) ) :
        wp_enqueue_script( 'dkret-commentform-validation', THEME_JS .'/dkret-commentform-validation.js', array( 'jquery' ), '3.6', true );
    endif;
}
endif;

function dkret_add_header_image() {
    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
    if ( is_singular() && has_post_thumbnail( $post->ID ) && ( $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail') ) && $image[1] >= HEADER_IMAGE_WIDTH ) :
        // Houston, we have a new header image!
        $header = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else :
        $header = '<img src="'.get_header_image() .'" width="'. HEADER_IMAGE_WIDTH .'" height="'. HEADER_IMAGE_HEIGHT .'" alt="" />';
    endif;

    echo $header;
}


if ( ! function_exists( 'dkret_add_body_classes' ) ) :
/**
 * add OS and page_ID classes to body_class()
 *
 * Code used from the fantastic hybrid theme framework (http://themehybrid.com/archives/2008/11/hybrid-wordpress-theme-framework)
 * Major props to Ptah Dunbar (http://wpframework.com)
 *
 * @since 3.6
 */
function dkret_add_body_classes( $classes ) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome;
    
	$browsers = array(
        'gecko' => $is_gecko,
        'opera' => $is_opera,
        'lynx' => $is_lynx,
        'ns4' => $is_NS4,
        'safari' => $is_safari,
        'chrome' => $is_chrome,
        'msie' => $is_IE
    );
    
	foreach ( $browsers as $key => $value ) :
		if ( $value ) :
			$classes[] = $key;
			break;
		endif;
	endforeach;

    return $classes;
}
endif;

if ( ! function_exists( 'dkret_add_menu_class' ) ) :
/**
 * Add ID and CLASS attributes to the first <ul> occurence in wp_page_menu()
 *
 * @since 3.6
 */
function dkret_add_menu_class( $ulclass ) { 
    return preg_replace( '/<ul>/', '<ul class="sf-menu page-menu">', $ulclass, 1 ); 
} 
endif;

if ( ! function_exists( 'dkret_admin_custom_header' ) ) :
/**
 * dkret_admin_custom_header() - Styles the custom header image admin page for use with dkret.
 * 
 * @since 3.0
 */
function dkret_admin_custom_header() {
?>
<style type="text/css">
    #headimg {
    <?php
        global $post;
        $header_image = dkret_random_header_image();
        if ( !empty( $header_image ) ) :
            $header_image = THEME_URI . '/library/images/headers/random.jpg';
        else :
            $header_image = HEADER_IMAGE;
        endif;
    ?>
    background: url(<?php echo $header_image; ?>) no-repeat !important;
    height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
    width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
    text-align: right;
    }
    #headimg h1
    {
    margin: 0px;
    padding: 25px 25px 5px;
    font-size: 36px;
    font-weight: normal;
    }
    #desc {
    font-size: 16px;
    padding: 0 25px 0;
    }
    #headimg h1 a
    {
    color: #<?php header_textcolor(); ?>;
    text-decoration: none;
    }
</style>
<?php
}
endif;

if ( ! function_exists( 'dkret_custom_header' ) ) :
/**
 * Function to display the Custom Header overwriting the default header styles.
 * 
 * @since 3.0
 */
function dkret_custom_header() { ?>
    
    <style type="text/css">

        <?php
            global $post;
            $random = dkret_random_header_image();
            
            // On singular view use featured image instead of the custom header image
            if ( is_singular() && has_post_thumbnail( $post->ID ) ) :
                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail');
                if ( $image[1] >= HEADER_IMAGE_WIDTH ) :
                    $header_image = $image[0];
                endif;

            // if at least one random header image is uploaded to "wp-content/uploads/headers" display it/them instead the original header
            elseif ( !empty( $random ) ) :
                $header_image = $random;
                
            // display the standard header
            else :
                $header_image = get_header_image();
            endif;
        ?>

        #header {
            background: url( <?php echo $header_image; ?> ) repeat-x;
            height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
            padding: 0 0 0 18px;
        }

        <?php if ( get_theme_mod('header_textcolor') == 'blank' || defined( 'NO_HEADER_TEXT' ) ) : ?>
            #site-title,
            #site-description {
                display: none;
            }
        <?php elseif ( get_theme_mod( 'header_textcolor' ) ) : ?>
            #site-title a,
            #site-description {
                color: #<?php header_textcolor(); ?>;
            }
        <?php endif; ?>

    </style>

<?php
}
endif;

if ( ! function_exists( 'dkret_random_header_image' ) ) :
/**
 * Function to return random image inside $picture_dir.
 * 
 * @since 3.0
*/
function dkret_random_header_image() {
    $picture_dir = @ dir( WP_CONTENT_DIR . '/uploads/headers' );

    if ( ! empty ( $picture_dir ) ) :
        while ( ( $file = $picture_dir->read() ) !== false ) :
            if ( ! preg_match( '|^\.+$|', $file ) && ! preg_match( '|^CVS$|', $file ) && preg_match( '/\.(gif|jpg|png)$/i', $file ) ) :
                $picture_files[] = $file;
            endif;
        endwhile;
    else:
        return false;
    endif;

    $size = count( $picture_files );

    if ( $size == 0 )
        return false;

    return ( WP_CONTENT_URL . '/uploads/headers/'. $picture_files[ rand( 0, $size-1 ) ] );
}
endif;

if ( ! function_exists( 'get_dkret_option' ) ) :
/**
 * Function to get a dkret3 Option
 * 
 * @since 3.0
 */
function get_dkret_option( $option ) {
    $get_dkret_options = get_option( 'dkret3' );
    return $get_dkret_options[$option];
}
endif;

if ( ! function_exists( 'dkret_option' ) ) :
/**
 * Function to echo a dkret3 option
 * 
 * @since 3.0
 */
function dkret_option( $option ) {
    $get_dkret_options = get_option( 'dkret3' );
    echo esc_attr( $get_dkret_options[$option] );
}
endif;

if ( ! function_exists( 'dkret_add_menu' ) ) :
/**
 * Function to add the dkret Settings page to the WordPress admin panel.
 * 
 * @since 3.6
 */
function dkret_add_menu() {
    $data = get_theme_data( STYLESHEETPATH . '/style.css' );
    $page = add_theme_page(
        esc_attr( $data['Name'] ) . ' ' . __( 'Settings', 'dkret' ),
        esc_attr( $data['Name'] ) . ' ' . __( 'Settings', 'dkret' ),
        'edit_themes',
        'theme-settings',
        'dkret_theme_settings' );

    add_action( "admin_print_styles-$page", 'dkret_inject_styles' );
}
endif;

if ( ! function_exists( 'dkret_theme_settings' ) ) :
/**
 * Function to displays the Settings page.
 * 
 * @since 3.0
 */
function dkret_theme_settings() {
    if ( current_user_can( 'edit_themes' ) )
        include( THEME_ADMIN . '/theme-settings.php' );
}
endif;

if ( ! function_exists( 'dkret_inject_styles' ) ) :
/**
 * dkret_inject_styles() - Adds CSS to the dkret Settings page.
 * 
 * @since 3.0
*/
function dkret_inject_styles() {
    wp_enqueue_style(
        'get_dkret_options',
        THEME_STYLES . '/settings.css',
        array(), false, 'screen'
    );
}
endif;

if ( ! function_exists( 'dkret_is_active_sidebars' ) ) :
/**
 * Function checks for Sidebars with active Widgets.
 * returns false if no Sidebar has active Widgets
 * 
 * @since 3.0
 */
function dkret_is_active_sidebars() {
	$sidebars_widgets = get_option( 'sidebars_widgets', array() );	

    unset( $sidebars_widgets['wp_inactive_widgets'] );
    unset( $sidebars_widgets['array_version'] );

    foreach ( $sidebars_widgets as $sidebar )
        if ( ! empty( $sidebar ) )
            return true;
                
	return false;
}
endif;

if ( ! function_exists( 'dkret_register_sidebars' ) ) :
/**
 * Function to register all widgetized dkret3 Sidebars.
 * 
 * @since 3.6
 */
function dkret_register_sidebars() {

    register_sidebar( array(
        'name' => __( 'Primary Sidebar','dkret' ),
        'id' => 'primary-sidebar',
        'description' => __( 'First main widget area, most often used as a sidebar.', 'dkret' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );

    register_sidebar(array(
        'name' => __( 'Secondary Sidebar','dkret' ),
        'id' => 'secondary-sidebar',
        'description' => __( 'Second main widget area, most often used as a sidebar.', 'dkret' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );

    register_sidebar(array(
        'name' => __( '1st Subsidiary Sidebar','dkret' ),
        'id' => '1st-subsidiary-sidebar',
        'description' => __( 'Widget area below the content, most often used as the 1st column.', 'dkret' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );

    register_sidebar(array(
        'name' => __( '2nd Subsidiary Sidebar','dkret' ),
        'id' => '2nd-subsidiary-sidebar',
        'description' => __( 'Widget area below the content, most often used as the 2nd column.', 'dkret' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );

    register_sidebar(array(
        'name' => __( '3rd Subsidiary Sidebar','dkret' ),
        'id' => '3rd-subsidiary-sidebar',
        'description' => __( 'Widget area below the content, most often used as the 3rd column.', 'dkret' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );
    
    register_sidebar(array(
        'name' => __( '404','dkret' ),
        'id' => '404',
        'description' => __( 'Widget area for a customized 404 Error Page.', 'dkret' ),
		'before_widget' => '<div id="%1$s" class="%2$s widget">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgettitle">',
		'after_title' => '</h3>' ) );

    register_sidebar(array(
        'name' => __( '1st Sidebar Column','dkret' ),
        'id' => '1st-sidebar-column',
        'description' => __( 'Add widgets to the 1st column of the Sidebar Columns Widget.', 'dkret' ),        
       	'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );
  
    register_sidebar(array(
        'name' => __( '2nd Sidebar Column','dkret' ),
        'id' => '2nd-sidebar-column',
        'description' => __( 'Add widgets to the 2nd column of the Sidebar Columns Widget.', 'dkret' ),    
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
       	'after_widget' => "</div>",
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>' ) );
}
endif;

if ( ! function_exists( 'dkret_register_widgets' ) ) :
/**
 * dkret_register_widgets()
 * 
 * @since 3.6
 */
function dkret_register_widgets() {

    // First properly unregister Wordpress Widgets
    unregister_widget( 'WP_Widget_Pages' );
    unregister_widget( 'WP_Nav_Menu_Widget' );
    unregister_widget( 'WP_Widget_Text' );
    unregister_widget( 'WP_Widget_Links' );
    unregister_widget( 'WP_Widget_Meta' );
    unregister_widget( 'WP_Widget_Calendar' );

    // Now register dkret specific Widgets
    register_widget( 'DKRET_Widget_Text' );
    register_widget( 'DKRET_Widget_Links' );
    register_widget( 'DKRET_Widget_Meta' );
    register_widget( 'DKRET_Widget_Pages' );
    register_widget( 'DKRET_Nav_Menu_Widget' );
    register_widget( 'DKRET_Widget_RSS_Links' );
    register_widget( 'DKRET_Widget_Calendar' );
    register_widget( 'DKRET_Widget_Sidebar_Columns' );

    if ( get_option( 'dkret_adsense_block_2' ) )
        register_widget( 'DKRET_Widget_AdSense' );
}
endif;

if ( ! function_exists( 'dkret_default_options' ) ) :
/**
 * This function creates all of the default theme settings and adds them to a single array. By saving 
 * them in one array, the function only creates one setting in the {$wpdb->prefix}_options table.
 *
 * @since 3.6
 */
function dkret_default_options() {

        $defaults = array(
            'layout_file' => 'Default-Layout',
            'preview_comments' => true,
            'validate_comments' => true,        
            'summary' => false,
            'rss_link' => true,
            'gallery_link' => true,
            'show_navigation' => true,
            'gallery_cat_id' => '-1',
            'home_link_name' => __('Start','dkret'),
            'remove_meta_information' => false,
            'remove_settings' => false,
            'hide_adsense' => false,
            'adsense_before' => false,
            'adsense_after' => false,
            'header_link' => true,
            'footer_text' => '<span class="copyright">' . __( 'Copyright &#169; [the-year] [site-link].', 'dkret' ) . '</span>' . " " . '<span class="credit">' . __( 'Powered by [wp-link] and [theme-link].', 'dkret' ) . '</span> [separator] [login-link]'
        );
        
        /* If there is a child theme active, add the [child-link] shortcode to the $footer_insert. */
        if ( is_child_theme() )
            $defaults['footer_text'] = '<span class="copyright">' . __( 'Copyright &#169; [the-year] [site-link].', 'dkret' ) . '</span>' . " " . '<span class="credit">' . __( 'Powered by [wp-link], [theme-link] and [child-link].', 'dkret' ) . '</span> [separator] [login-link]';

        return apply_filters( 'dkret_default_options_args', $defaults );
}
endif;


if ( ! function_exists( 'dkret_setup_options' ) ) :
/**
 * Function to add Options needed to run dkret to the Database.
 * 
 * @since 3.6
 */
function dkret_setup_options() {
    $options = get_option( 'dkret3' );
    if ( empty( $options ) ) :
        $defaults = dkret_default_options();
        update_option( 'dkret3' , $defaults );

    endif;
}
endif;

if ( ! function_exists( 'dkret_update_options' ) ) :
/**
 * Function to change and update changed options
 * 
 * @since 3.0
 */
function dkret_update_options() {
    check_admin_referer( 'theme-settings' );

    if ( ! current_user_can( 'edit_themes' ) )
        wp_die( __( 'You are not authorised to perform this operation.', 'dkret' ) );

    if ( isset( $_POST['restore-defaults'] ) ) :
        dkret_reset_options();
        wp_redirect( admin_url( 'themes.php?page=theme-settings&defaults=true' ) );
        return;

    else :
        $options = get_option( 'dkret3' );

        /* Loop through each of the default settings and match them with the posted settings. */
        foreach ( dkret_default_options() as $key => $value ) :
            if ( !empty( $_POST[$key] ) ) 
                $options[$key] = stripslashes( $_POST[$key] );
            else
                $options[$key] = false;
        endforeach;

        $adsense_options = array(
            'adsense_block_1',
            'adsense_block_2',
            'adsense_block_3',
            'adsense_block_4',
            'adsense_block_5',
            'adsense_block_6'
        );
        
        /* Loop through each of the adsense blocks, match them with the posted settings and save their options. */
        foreach ( $adsense_options as $field => $val ) :
            if ( $_POST[$val] == '' )
                delete_option( 'dkret_' . $val );
            elseif ( isset( $_POST[$val] ) )
                update_option( 'dkret_' . $val, stripslashes( $_POST[$val] ) );
        endforeach;
                
        update_option( 'dkret3', $options );

    endif;
    wp_redirect( admin_url( 'themes.php?page=theme-settings&updated=true' ) );
}
endif;

if ( ! function_exists( 'dkret_delete_options' ) ) :
/**
 * Function to delete all options saved to the WordPress tables
 * 
 * @since 3.6
 */
function dkret_delete_options() {
    if ( ! get_dkret_option( 'remove_settings' ) )
        return;
        
    delete_option( 'dkret3' );
    delete_option( 'dkret_adsense_block_1' );
    delete_option( 'dkret_adsense_block_2' );
    delete_option( 'dkret_adsense_block_3' );
    delete_option( 'dkret_adsense_block_4' );
    delete_option( 'dkret_adsense_block_5' );
    delete_option( 'dkret_adsense_block_6' );
}
endif;

if ( ! function_exists( 'dkret_reset_options' ) ) :
/**
 * Function to revert all saved options to default values
 * 
 * @since 3.6
 */
function dkret_reset_options() {
    delete_option( 'dkret3' );
    dkret_setup_options();
}
endif;
?>